package com.atguigu.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.atguigu.pojo.Employee;
import com.atguigu.pojo.Result;
import com.atguigu.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @PostMapping("add")
    public Result add(@RequestBody Employee employee) {
        employeeService.add(employee);
        System.out.println("添加的数据是  "+employee);
        return Result.success();
    }
    @PutMapping("update")
    public Result update(@RequestBody Employee employee) {
        System.out.println("---------------------");
        System.out.println(employee);
        System.out.println("---------------------");
        employeeService.update(employee);
        return Result.success();
    }

    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        employeeService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        employeeService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("selectAll")
    public Result selectAll(Employee employee) {
        List<Employee> list = employeeService.selectAll(employee);
        return Result.success(list);
    }

    @GetMapping("selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        return Result.success(employeeService.selectById(id));
    }

    @GetMapping("selectByOne")
    public Result selectByOne(@RequestParam Integer id) {
        return Result.success(employeeService.selectById(id));
    }

    @GetMapping("selectPage")
    public Result selectPage(Employee employee,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(employeeService.selectPage(employee, pageNum, pageSize));
    }

    
    @GetMapping("export")
    public void export(HttpServletResponse response) throws IOException {
        // 拿到所有员工数据
        List<Employee> employeeList = employeeService.selectAll(null);
        // 构建ExcelWriter 在内存操作导出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        // 设置中文表头
        writer.addHeaderAlias("username", "账号");
        writer.addHeaderAlias("name", "名称");
        writer.addHeaderAlias("sex", "性别");
        writer.addHeaderAlias("no", "工号");
        writer.addHeaderAlias("age", "年龄");
        writer.addHeaderAlias("description", "个人介绍");
        writer.addHeaderAlias("departmentName", "部门");

        writer.setOnlyAlias(true); //只导出别名的

        //写出数据到writer
        writer.write(employeeList, true);

        // 设置输出的文件的名称  以及输出流的头信息
        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("员工信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        // 写出到输出流后关闭

        ServletOutputStream os = response.getOutputStream();
        writer.flush(os);
        writer.close();
    }

    @PostMapping("import")
    public Result importExcel(MultipartFile file) throws IOException {
        // 拿到数据流构建reader
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);

        // 读入 excel数据
        reader.addHeaderAlias("账号", "username");
        reader.addHeaderAlias("名称", "name");
        reader.addHeaderAlias("性别", "sex");
        reader.addHeaderAlias("工号", "no");
        reader.addHeaderAlias("年龄", "age");
        reader.addHeaderAlias("个人介绍", "description");
        reader.addHeaderAlias( "部门", "department_id");
        List<Employee> employeeList = reader.readAll(Employee.class);

        // 写入 excel数据到数据库
        employeeList.forEach(employee -> employeeService.add(employee));
        return Result.success();

    }
}
